// pages/sell/sell.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: [],
    price: [],
    imgs: [],
    count: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  nameInput: function (e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  priceInput: function (e) {
    console.log(e)
    this.setData({
      price: e.detail.value
    })
  },
  bindUpload: function (e) {
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 3
        break
      case 1:
        this.data.count = 2
        break
      case 2:
        this.data.count = 1
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          that.setData({
            imgs: tempFilePaths
          })
        }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除",
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.imgs.length; i++) {
            if (i == e.currentTarget.dataset.index) {
              that.data.imgs.splice(i, 1)
            }
          }
          that.setData({
            imgs: that.data.imgs
          })
        } else if (res.cancel) {
          console.log("用户点击取消")
        }
      }
    })
  },
  formSubmit: function (e) {
    console.log(this.data)
    if (!e.detail.value.name) {
      wx.showModal({
        title: "提示",
        content: "请输入名称"
      })
    }
    else if (!e.detail.value.price) {
      wx.showModal({
        title: "提示",
        content: "请输入价格"
      })
    }
    else if (this.data.imgs.length == 0) {
      console.log("1")
      wx.showModal({
        title: "提示",
        content: "请选择图片"
      })
    }
    else {
      wx.request({
        url: 'http://192.168.174.166:3000/up',
        method: "POST",
        header: { 'context-Type': 'application/x-www-form-urlencoded' },
        body: {
          name: this.data.name,
          price: this.data.price,
          imgs: this.data.imgs,
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: "成功",
            icon: "success",
            duration: 2000
          })
        }
      })
    }
  }
})